package com.socialbank.web.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.socialbank.web.bean.User;
import com.socialbank.web.dto.BankMoneyCodeDto;
import com.socialbank.web.dto.BankMoneyDto;
import com.socialbank.web.dto.BankRewardDto;
import com.socialbank.web.dto.PointExchangeRewardDto;
import com.socialbank.web.dto.UserPointDto;

public interface PointMapper {

	@Select("SELECT t0.user_id AS userId, t0.last_days AS lastDays, t1.get_point AS getPoint, t0.create_date AS createDate FROM usr_sign_in t0 LEFT JOIN usr_point_record t1 ON t0.id = t1.id WHERE t0.user_id = #{userId} AND t1.get_point>0 ORDER BY t0.create_date DESC LIMIT #{pageNo},#{pageSize}")
	List<UserPointDto> getPointDetail(UserPointDto userPointDto);
	
	@Insert("INSERT INTO bank_rewards ( id, NAME, price, point, img, count, category_id, dept_id, `describe`, create_date ) VALUES (#{id}, #{name}, #{price}, #{point}, #{img}, #{count} #{categoryId}, #{deptId}, #{describe}, NOW())")
	void addReward(BankRewardDto bankRewardDto);

	@Delete("DELETE FROM bank_rewards WHERE id=#{id}")
	int deleteReward(BankRewardDto bankRewardDto);

	@Update("UPDATE bank_rewards SET name=#{name}, price=#{price}, point=#{point}, img=#{img}, count=#{count}, category_id=#{categoryId}, dept_id=#{deptId}, `describe`=#{describe}, create_date=NOW() WHERE id=#{id} ")
	int updateReward(BankRewardDto bankRewardDto);
	
	@Select("SELECT id, user_id AS userId, reward_id AS rewardId, reward_name AS rewardName, reward_price AS rewardPrice, reward_point AS rewardPoint, reward_type AS rewardType, reward_code AS rewardCode, used, expire_time AS expireTime, create_time AS createTime FROM usr_exchange_record WHERE reward_code = #{rewardCode}")
	PointExchangeRewardDto checkRewardCode(PointExchangeRewardDto pointExchangeRewardDto);

	@Select("SELECT id, NAME, price, point, img, category_id AS categoryId, dept_id AS deptId, `describe`, create_date AS createDate FROM bank_rewards WHERE id IN ( SELECT reward_id FROM usr_exchange_record WHERE reward_code = #{rewardCode} )")
	BankRewardDto getRewardGoodbyCode(PointExchangeRewardDto pointExchangeRewardDto);

	@Update("UPDATE usr_exchange_record SET used = '0' WHERE reward_code = #{rewardCode} ")
	int completeExchange(PointExchangeRewardDto pointExchangeRewardDto);

	@Select("SELECT id, dept_id AS deptId, product_code AS productCode, product_short_name AS productShortName, collect_period AS collectPeriod, deadline, product_validity_end AS productValidityEnd, earnings_type AS earningsType, earnings_year AS earningsYear, money, sell_channel AS sellChannel, risk_type AS riskType, product_type AS productType, batch, point FROM bank_money_manage LIMIT #{pageNo},#{pageSize}")
	List<BankMoneyDto> getExclusiveFinancial(BankMoneyDto bankMoneyDto);

	@Insert("INSERT INTO bank_money_manage ( id, dept_id, product_code, product_short_name, collect_period, deadline, product_validity_end, earnings_type, earnings_year, money, sell_channel, risk_type, product_type, batch, point ) VALUES ( #{id}, #{deptId}, #{productCode}, #{productShortName}, #{collectPeriod}, #{deadline}, #{productValidityEnd}, #{earningsType}, #{earningsYear}, #{money}, #{sellChannel}, #{riskType}, #{productType}, #{batch}, #{point} )")
	int addExclusiveFinancial(BankMoneyDto bankMoneyDto);

	@Delete("DELETE FROM bank_money_manage WHERE id=#{id}")
	int deleteExclusiveFinancial(BankMoneyDto bankMoneyDto);

	@Update("UPDATE bank_money_manage SET dept_id=#{deptId}, product_code=#{productCode}, product_short_name=#{productShortName}, collect_period=#{collectPeriod}, deadline=#{deadline}, product_validity_end=#{productValidityEnd}, earnings_type=#{earningsType}, earnings_year=#{earningsYear}, money=#{money}, sell_channel=#{sellChannel}, risk_type=#{riskType}, product_type=#{productType}, batch=#{batch}, point=#{point} WHERE id=#{id} ")
	int updateExclusiveFinancial(BankMoneyDto bankMoneyDto);

	@Insert("INSERT INTO bank_money_code (id, user_id, code, used, create_date) VALUES (#{id}, #{userId}, #{code}, #{used}, NOW())")
	int addBankMoneyCode(BankMoneyCodeDto bankMoneyCodeDto);

	@Select("SELECT id, acct_no AS acctNo, NAME AS `name`, sex, phone, avatar, birthday, address, access_token AS accessToken, PASSWORD, open_id AS openId, subscribe, dept_id AS deptId, sum_point AS sumPoint, create_date AS createDate, update_date AS updateDate, extra FROM usr_user WHERE id IN ( SELECT user_id FROM usr_exchange_record WHERE reward_type = 2 ) LIMIT #{pageNo},#{pageSize}")
	List<User> getExchangeBankMoneyOfUserList(BankMoneyDto bankMoneyDto);


	@Select("SELECT id, sum_point as sumPoint, create_date as createDate	FROM usr_user WHERE id = #{userId}")
	UserPointDto getUserSumpoint(PointExchangeRewardDto pointExchangeRewardDto);
	
	@Update("UPDATE usr_user	SET		sum_point= #{sumPoint}	WHERE id = #{userId} ")
	void updateSumPoint(UserPointDto userPointDto);
	
	@Insert("INSERT INTO usr_exchange_record ( id, user_id, reward_id, reward_name, reward_price, reward_count, reward_point, reward_type, reward_code, used, expire_time, create_time ) VALUES ( #{id}, #{userId}, #{rewardId}, #{rewardName}, #{rewardPrice}, #{rewardCount}, #{rewardPoint}, #{rewardType}, #{rewardCode}, #{used}, #{expireTime}, NOW() )")
	void savePointExchangeReward(PointExchangeRewardDto pointExchangeRewardDto);

	@Select("SELECT count FROM bank_rewards where id = #{id} ")
	Integer getBankRewardCount(Long id);

	@Update("UPDATE bank_rewards	SET		count=#{count}	WHERE  id = #{id}")
	void updateBankRewardCount(Integer count, Long id);
}
